package com.service.impl;

import com.dao.UserMapper;
import com.domain.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import com.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    /*
     * 用户分页以及多条件查询
     * */
    @Override
    public PageInfo<User> findAllUserByPage(UserVO userVO) {
        PageHelper.startPage(userVO.getCurrentPage(),userVO.getPageSize());
        List<User> users = userMapper.findAllUserByPage(userVO);

        return new PageInfo<>(users);

    }
    /*
     * 用户状态设置
     * */
    @Override
    public void updateUserStatus(int id, String status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);

        Date date = new Date();
        user.setUpdate_time(date);

        userMapper.updateUserStatus(user);
    }
    /*
     * 用户登陆
     * */
    @Override
    public User login(User user) throws Exception {
        User user1 = userMapper.login(user);
        if (user1 != null && Md5.verify(user.getPassword(),"lagou",user1.getPassword())){
            return user1;
        }else return null;
    }
    /*
     * 根据用户id查询相关的角色信息
     * */
    @Override
    public List<Role> findUserRelationRoleById(Integer id) {
        return userMapper.findUserRelationRoleById(id);
    }
    /*
     * 分配角色
     * */
    @Override
    public void userContextRole(UserVO userVO) {
        userMapper.deleteUserContextRole(userVO.getUserId());

        List<Integer> roleIdList = userVO.getRoleIdList();
        User_Role_relation userRoleRelation = new User_Role_relation();
        for (Integer integer : roleIdList) {
            userRoleRelation.setUserId(userVO.getUserId());
            userRoleRelation.setRoleId(integer);
            Date date = new Date();
            userRoleRelation.setCreatedTime(date);
            userRoleRelation.setUpdatedTime(date);
            userRoleRelation.setCreatedBy("system");
            userRoleRelation.setUpdatedby("system");
            userMapper.userContextRole(userRoleRelation);
        }
    }
    /*
     * 获取用户权限，进行菜单动态展示
     * */
    @Override
    public ResponseResult getUserPermissions(Integer userId) {
        //获取当前用户所拥有的角色
        List<Role> roleList = userMapper.findUserRelationRoleById(userId);
        ArrayList<Integer> roleIds = new ArrayList<>();
        for (Role role : roleList) {
            roleIds.add(role.getId());
        }

        //根据角色id查询父级菜单
        List<Menu> menus = userMapper.findParentMenuByRoleId(roleIds);
        for (Menu menu : menus) {
            //根据pid查询对应的子菜单信息
            List<Menu> subMenuList = userMapper.findSubMenuByPid(menu.getId());
            menu.setSubMenuList(subMenuList);
        }

        //根据角色id查找相关的资源信息
        List<Resource> resourceList = userMapper.findResourceByRoleId(roleIds);

        Map<String, Object> map = new HashMap<>();
        map.put("menuList",menus);
        map.put("resourceList",resourceList);
        map.put("success",true);

        return new ResponseResult(true,200,"获取用户权限信息成功",map);

    }
}
